package com.vates.reservas.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vates.reservas.dominio.Hotel;
import com.vates.reservas.dominio.TipoHabitacion;
import com.vates.reservas.service.HotelService;

/**
 * Handles and retrieves the main requests
 */
@Controller
@RequestMapping(value = "/habitacion/eliminarHabitacion")
public class HabitacionEliminarController {

	protected static Logger logger = Logger.getLogger("controller");

	@Autowired
	private HotelService hotelService;

	/**
	 * Handles and retrieves the AJAX Add page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String gethotelEditar(Model model) {
		model.addAttribute(new Hotel());
		return "habitacion/eliminarHabitacion";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Hotel hotel, BindingResult result) {
		if (result.hasErrors()) {
			return "habitacion/eliminarHabitacion";
		}
		return "redirect:/habitacion/eliminarHabitacion/" + hotel.getId();
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String gethotelModificar(@PathVariable Long id, Model model) {
		Hotel hotel= hotelService.busquedaPorId(id);
		model.addAttribute(hotel);
		model.addAttribute(new TipoHabitacion());
		return "habitacion/eliminar";
	}
	@RequestMapping(method=RequestMethod.POST, value="/eliminar/confirmar")
	public String modificar(HttpServletRequest request) {

		System.out.println("Llega al controlador a borrar");
		System.out.println("Hotel: " + request.getParameter("nroHotel"));
		System.out.println("Habitacion: " + request.getParameter("radio"));
		hotelService.eliminarHabitacion(Long.parseLong(request.getParameter("nroHotel")), Long.parseLong(request.getParameter("radio")));
		return "redirect:/hotel/busquedaPorId/"+request.getParameter("nroHotel");
	}
}